﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using microCRM.Domain;

namespace microCRM
{
    public partial class SysCareAddPlan : Form
    {
        public delegate void DataAdded(object sender, EventArgs e);
        public event DataAdded DataAddedEvent;
        public SysCareAddPlan()
        {
            InitializeComponent();
        }

        private void btn_Save_Click(object sender, EventArgs e)
        {
            if (this.txtbx_PlanName.Text.Trim() == "")
            {
                this.txtbx_PlanName.BackColor = Color.Red;
                MessageBox.Show("Требуемые поля не заполнены! Проверьте правильность заполнения полей!", "Внимание", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else 
            {
                this.txtbx_PlanName.BackColor = Color.White;
                Plan plan = new Plan();
                plan.Name = this.txtbx_PlanName.Text;
                plan.MonthlyFee = (double)this.nud_MonthlyFee.Value;
                plan.PerSecondBilling = this.cbx_PerSecond.Checked;
                plan.VoiceCallCost = (double)this.nud_VoiceCost.Value;
                plan.SmsCost = (double)this.nud_SMSCost.Value;
                plan.MmsCost = (double)this.nud_MMSCost.Value;
                plan.DataCost = (double)this.nud_InternetCost.Value;
                plan.FreeCallMinutes = (int)this.nud_FreeVoiceCalls.Value;
                plan.FreeSms = (int)this.nud_FreeSMS.Value;
                plan.FreeData = (double)this.nud_FreeInternet.Value;
                plan.InternationalCallsCost = (double)this.nud_InternationalCallsCost.Value;

                try
                {
                    DBHelper.CreatePlan(plan);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Внимание, произошла ошибка! Свяжитесь с разработчиком!\nОписание проблемы:\n\n" + ex.Message + "\n\n" + ex.StackTrace, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                if (MessageBox.Show("Данные были успешно сохранены!", "", MessageBoxButtons.OK, MessageBoxIcon.Information) == DialogResult.OK)
                {
                    DataAddedEvent(this, new EventArgs());
                    this.Close();
                    
                }

            }
            
        }

        private void btn_Cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
